package com.topscomm.mis.cbb.pm.service.chain.handler;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboApprovalNodeEntity;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.ICboApprovalNodeInsService;
import com.topscomm.cbo.service.ICboApprovalNodeService;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.cbo.service.impl.CboApprovalNodeService;
import com.topscomm.mis.cbb.pm.api.ITaskService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmTaskEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskService;
import com.topscomm.mis.cbb.pm.base.service.impl.CbbPmTaskService;
import com.topscomm.mis.cbb.pm.service.chain.TaskCompleteHandler;
import com.topscomm.mis.cbb.pm.service.impl.TaskService;
import com.topscomm.mis.cbb.pm.util.PmSystemConst;
import com.topscomm.pub.util.*;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.tap.common.TapSystemConst;
import com.topscomm.tap.util.ScriptUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 责任链其他类型任务处理
 * @author: wangxinhua
 * @date: 2023/6/16 10:07
 * @modify:
 */
@Component
@Scope(value = "singleton")
public class OtherHandler extends TaskCompleteHandler {
//    @Autowired
//    protected ICbbPmTaskService cbbPmTaskService;
//    @Autowired
//    protected ICboApprovalNodeService cboApprovalNodeService;
//    @Autowired
//    protected ICboApprovalNodeInsService cboApprovalNodeInsService;
//    @Autowired
//    protected ITaskService taskService;

    @Override
    public void completeTask(Map<String, Object> taskInfo) {
        CbbPmTaskService cbbPmTaskService =  (CbbPmTaskService) SpringUtil.getBean("cbbPmTaskService");
        CboApprovalNodeService cboApprovalNodeService =  (CboApprovalNodeService) SpringUtil.getBean("cboApprovalNodeService");
        if (PmSystemConst.TaskRelation.OTHER == ConvertUtil.convertToInt(taskInfo.get(CbbPmTaskEntity.FieldRelation))){
            //处理其他类型任务，其他类型完成当前任务
            //更新任务状态
            taskInfo.put(CbbPmTaskEntity.FieldStatus, PmSystemConst.TaskStatus.COMPLETE);
            taskInfo.put(CbbPmTaskEntity.FieldRealendtime, new Date());
            taskInfo.put(CbbPmTaskEntity.FieldId, taskInfo.get(CbbPmTaskEntity.FieldId));
            cbbPmTaskService.update(taskInfo);
            //如果是自循环节点
//            CboApprovalNodeEntity approvalNodeEntity = cboApprovalNodeService.queryById(ConvertUtil.convertToString(taskInfo.get(CbbPmTaskEntity.FieldNodeid)));
//            if (TapSystemConst.TapApprovalNodeTypeEnum.AutoLoopNode == approvalNodeEntity.getNodetype()) {
//                this.setNextApprovalTask(taskInfo, approvalNodeEntity);
//            }
        } else if (this.getNextHandler() != null) {
            this.getNextHandler().completeTask(taskInfo);
        } else {
            // 处理无法处理的请求
            throw new ServiceException("任务结束失败:" + taskInfo.toString());
        }
    }

    /***
     * @description: 自循环节点处理下一步
     * @param taskInfo
     * @param approvalNodeEntity
     * @return void
     * @author: wangxinhua
     * @date: 2023/6/20 14:42
     * @modify:
     */
    private void setNextApprovalTask(Map<String, Object> taskInfo, CboApprovalNodeEntity approvalNodeEntity) {
        CbbPmTaskService cbbPmTaskService =  (CbbPmTaskService) SpringUtil.getBean("cbbPmTaskService");
        CboApprovalNodeInsService cboApprovalNodeInsService =  (CboApprovalNodeInsService) SpringUtil.getBean("cboApprovalNodeInsService");
        TaskService taskService =  (TaskService) SpringUtil.getBean("taskService");
        CboApprovalNodeInsEntity taskNodeInsEntity = cboApprovalNodeInsService.queryObjectByWhere(ConvertUtil.convertToString(taskInfo.get(CbbPmTaskEntity.FieldNodeinsid)));
        if (taskNodeInsEntity == null) {
            return;
        }
        SessionUserBean approvalSessionUserBean = taskService.generateSessionUserBean("",taskNodeInsEntity.getApprovaluserid());
        String scriptContent = ScriptUtil.replaceScript(approvalNodeEntity.getPlanuserexp(), taskNodeInsEntity, approvalSessionUserBean, cboApprovalNodeInsService);
        List<Map<String, String>> listMapScript = (List<Map<String, String>>) JSONUtil.parseJSONString(scriptContent);
        Map<String, String> mapScript = listMapScript.get(0);
        if (mapScript.containsKey("StepLen")) {
            int endLen = ConvertUtil.convertToInt(mapScript.get("EndLen"));
            String extCol = ConvertUtil.convertToString(mapScript.get("ExtCol"));
            String extColValue = ConvertUtil.convertToString(taskNodeInsEntity.getValue(extCol));
            // 结束循环
            if (extColValue.length() <= endLen){
                return;
            }
            // 节点审批通过
            cbbPmTaskService.executeApprove(ConvertUtil.convertToString(taskInfo.get(CbbPmTaskEntity.FieldNodeinsid)), new Date(), "WBS任务完成", "1", "0",
                    approvalSessionUserBean, 0L);
            //获取当前审批的自循环节点实例
            StringBufferProxy whereSql = new StringBufferProxy();
            whereSql.appendSingle(" {0}={1} ", CboApprovalNodeInsEntity.FieldNodeid, approvalNodeEntity.getId());
            whereSql.appendSingle(" and {0}='{1}' ", CboApprovalNodeInsEntity.FieldState, TapSystemConst.TapApprovalNodeStateEnum.Approveing);
            whereSql.appendSingle(" and {0}={1} ", CboApprovalNodeInsEntity.FieldSourceid, taskNodeInsEntity.getSourceid());
            whereSql.appendSingle(" and {0}='{1}' ", CboApprovalNodeInsEntity.FieldSourcetype, taskNodeInsEntity.getSourcetype());
            cbbPmTaskService.updateByWhere(taskInfo, whereSql.toString());
            List<Map<String, Object>> nodeInsList = cboApprovalNodeInsService.queryMapByWhere(whereSql.toString(), "queryNodeInsWithNodeType");
            if (nodeInsList.size()<=0) {
                return;
            }
            //更新任务信息
            whereSql.clear();
            whereSql.appendSingle(" {0}='{1}' ", CbbPmTaskEntity.FieldProjectuucode, taskInfo.get(CbbPmTaskEntity.FieldProjectuucode));
            whereSql.appendSingle(" and {0}='{1}' ", CbbPmTaskEntity.FieldNodeid, taskInfo.get(CbbPmTaskEntity.FieldNodeid));
            whereSql.appendSingle(" and {0}={1} ", CbbPmTaskEntity.FieldCurrently, PmSystemConst.CboYesNo.YES);
            whereSql.appendSingle(" and {0}='{1}' ", CbbPmTaskEntity.FieldResponsibleusercode, nodeInsList.get(0).get("approvalusercode"));
            Map<String, Object> updateMap = new HashMap<>();
            updateMap.put(CbbPmTaskEntity.FieldNodeinsid, nodeInsList.get(0).get(CbbPmTaskEntity.FieldId));
            cbbPmTaskService.updateByWhere(updateMap, whereSql.toString());
        }
    }
}
